package com.im.partchat.mvp.persenter;


import android.support.v4.app.Fragment;

import com.im.partchat.base.IBaseView;
import com.im.partchat.bean.main.MainNewData;
import com.im.partchat.mvp.model.news.NewsModel;
import com.im.partchat.mvp.model.news.NewsModelImpl;
import com.im.partchat.utils.rxlifecycle.compact.RxLifecycleCompact;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by flyou on 2017/6/30.
 * VersionCode: 1
 * Desc:
 */

public class GetMainDataPersenter<T> extends BasePresenter {
    private IBaseView<MainNewData> baseView;
    private NewsModel newsModel;
    private Fragment context;

    public GetMainDataPersenter(IBaseView<MainNewData> baseView, Fragment fragment) {
        this.baseView = baseView;
        this.context = fragment;
        this.newsModel = new NewsModelImpl();
    }

    public void getMainData() {
        responseCompose(newsModel.getMainNew(), baseView, "获取首页数据失败")
                .compose(RxLifecycleCompact.bind(context).<MainNewData>withObservable())
                .subscribe(new Observer<MainNewData>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        baseView.showLoading("正在获取数据");
                    }

                    @Override
                    public void onNext(MainNewData value) {

                        baseView.onLoadDataSuccess(value, "");
                    }

                    @Override
                    public void onError(Throwable e) {
                        baseView.hideLoading();
                        baseView.showError(NetErrorObservable(e));
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        baseView.hideLoading();
                    }
                });
    }

    
}
